import {defineStore} from 'pinia'
import tool from "@/utils/tool";
export const useGlobal = defineStore('globalStore', {
    state: () => {
        return {
            ismobile: false,
            // v3
            width: 0,
            height: 0,
            vgridHeaderHeight: 0, // 表格上面搜索栏动态高度
            areaList: [],
            routeList: [],
            currentRoute: {},
            storageList: [],
            jsonMenuList: [], // 有权限的菜单
            menus: [],
            saleBills: 0,
            systemParams: {}
        }
    },
    actions: {
        setCurrentRoute (route) {
            if(this.currentRoute.id !== route.id)
                this.currentRoute = route
        },
        addRoute (route) {
            let ids = tool.xe.pluck(this.routeList, 'id')
            if(ids.indexOf(route.id) !== -1) {
                this.currentRoute = route
            }
            else {
                this.routeList.push(route)
                this.currentRoute = route
            }
        },
        removeRoute (route) {
            let cIndex = -1
            for (let i = 0; i < this.routeList.length; i++) {
                if(this.routeList[i].id === route.id) {
                    cIndex = i
                    this.routeList.splice(i, 1)
                }
            }
            if(cIndex !== -1) {
                if(cIndex === 0) {
                    if(this.routeList.length > 0) {
                        this.currentRoute = tool.xe.clone(this.routeList[0], true)
                    } else {
                        this.currentRoute = {id: 0, routeId: 'home'}
                    }
                } else {
                    if(this.routeList.length > 0) {
                        this.currentRoute = tool.xe.clone(this.routeList[cIndex - 1], true)
                    }
                }
            }
        },
    }
})
